/*
 * Copyright (c) 2018 - 2028. www.vtuzx.com Inc. All Rights Reserved
 */
package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcPurOrderService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 采购订单
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/pur_order")
@RestController
public class XqcPurOrderController {
    /**
     * 采购订单服务
     */
    @Autowired
    private IXqcPurOrderService purOrderService;

    /**
     * 保存采购订单
     */
    @PostMapping("/save_pur_order")
    @VtuzxApi
    public Object savePurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        purOrderService.savePurOrder(_userId, name, param);
        return null;
    }

    /**
     * 删除采购订单
     */
    @PostMapping("/delete_pur_order")
    @VtuzxApi
    public Object deletePurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        purOrderService.deletePurOrder(_userId, name, param);
        return null;
    }

    /**
     * 审核采购订单
     */
    @PostMapping("/audit_pur_order")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "orderNo", label = "单号", notNull = true)
    })
    public Object auditPurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        purOrderService.auditPurOrder(_userId, name, orderNo);
        return null;
    }

    /**
     * 反审核采购订单
     */
    @PostMapping("/un_audit_pur_order")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "orderNo", label = "单号", notNull = true)
    })
    public Object unAuditPurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        purOrderService.unAuditPurOrder(_userId, name, orderNo);
        return null;
    }

    /**
     * 获取采购订单
     */
    @PostMapping("/get_pur_order_list")
    @VtuzxApi
    public Object getPurOrderList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, IVtuzxConst.PageSize);
        return purOrderService.getPurOrderList(_userId, param, pageIndex, pageSize);
    }

    /**
     * 获取采购订单
     */
    @PostMapping("/get_pur_order")
    @VtuzxApi(fields = {
            @VtuzxValidator(key = "orderNo", label = "单号", notNull = true)
    })
    public Object getPurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String orderNo = VtuzxUtil.getString(param, "orderNo");
        return purOrderService.getPurOrder(_userId, name, orderNo);
    }

    /**
     * 下载采购订单
     */
    @PostMapping("/download_pur_order")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadPurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        return purOrderService.downloadPurOrder(token, param);
    }

    /**
     * 保存采购订单结算
     */
    @PostMapping("/save_pur_order_settlement")
    @VtuzxApi
    public Object savePurSettlementBill(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return purOrderService.savePurOrderSettlement(_userId, name, param);
    }

    /**
     * 删除采购订单结算
     */
    @PostMapping("/delete_pur_order_settlement")
    @VtuzxApi
    public Object deletePurOrderSettlement(@SuppressWarnings("unused") @VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        List<String> ids = VtuzxUtil.getObject(param, "ids");
        purOrderService.deletePurOrderSettlement(ids);
        return null;
    }

    /**
     * 审核采购订单结算单
     */
    @PostMapping("/audit_pur_order_settlement")
    @VtuzxApi
    public Object auditPurOrderSettlement(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String id = VtuzxUtil.getString(param, "id");
        purOrderService.auditPurOrderSettlement(_userId, name, id);
        return null;
    }

    /**
     * 反审采购订单结算单
     */
    @PostMapping("/un_audit_pur_order_settlement")
    @VtuzxApi
    public Object unAuditPurOrderSettlement(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws VtuzxException {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String id = VtuzxUtil.getString(param, "id");
        purOrderService.unAuditPurOrderSettlement(_userId, name, id);
        return null;
    }

    /**
     * 获取采购订单结算单
     */
    @PostMapping("/get_pur_order_settlement")
    @VtuzxApi
    public Object getPurOrderSettlement(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return purOrderService.getPurOrderSettlement(_userId, name, param);
    }

    /**
     * 同步金蝶采购订单
     */
    @PostMapping("/sync_pur_order")
    @VtuzxApi
    public Object syncKingdeePurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        purOrderService.syncKingdeePurOrder(token, param);
        return null;
    }

    /**
     * 获取同步金蝶采购订单处理结果
     */
    @PostMapping("get_kingdee_pur_order")
    @VtuzxApi
    public Object getKingdeePurOrder(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        return purOrderService.getKingdeePurOrder(token, param);
    }
}





